﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;
using Aspose.BarCode;


namespace ECare.BUS
{
    public class ImageHelper
    {

        public static byte[] ConverToBase64(Bitmap bmp)
        {

        
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length);
            ms.Close();
            return arr;
        }

        public static Bitmap GetImageFromBase64(byte[] b)
        {
            MemoryStream ms = new MemoryStream(b);
            Bitmap bitmap = new Bitmap(ms);
            return bitmap;
        }

        public static Bitmap CreateBarcode(string code)
        {
            string currentDir = System.Environment.CurrentDirectory + @"\print\";


            if (!Directory.Exists(currentDir))
                Directory.CreateDirectory(currentDir);



            string barcodepng = currentDir + code + ".png";

            if (File.Exists(barcodepng))
                return (Bitmap)Bitmap.FromFile(barcodepng);

            var barcodeBuilder = new BarCodeBuilder
            {
                CodeText = code,
                SymbologyType = Symbology.Code128,
                Resolution = new Resolution(100f, 200f, ResolutionMode.Customized)
            };

            barcodeBuilder.Save(barcodepng, ImageFormat.Png);
            return (Bitmap)Bitmap.FromFile(barcodepng);

        }

    }
}
